package com.usefullc.system.service;

import com.usefullc.common.constants.SubPubConstants;
import com.usefullc.common.user.OnlineUser;
import com.usefullc.common.user.OnlineUserManager;
import com.usefullc.system.service.component.SystemConfigComponent;
import com.usefullc.system.service.component.redis.MessagePublisher;
import com.usefullc.system.service.component.redis.SubPubMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: Sampson
 * @date: 2022-02-01 16:45
 */
@Service
public class MessagePublishService extends AbstractService {


    @Autowired
    private SystemConfigComponent systemConfigComponent;

    @Autowired
    private MessagePublisher messagePublisher;

    public void publish(String key){
        SubPubMessage subPubMessage = new SubPubMessage();
        subPubMessage.setAppName(systemConfigComponent.getAppName());
//        subPubMessage.setKey(SubPubConstants.AUTH_DATA);
        subPubMessage.setKey(key);
        OnlineUser loginUser = OnlineUserManager.getLoginUser();
        if(loginUser != null){
            subPubMessage.setTenantId(loginUser.getTenantId());
        }
        messagePublisher.publish(subPubMessage);
    }

    public void publish(String key,Object data,Long tenantId){
        SubPubMessage subPubMessage = new SubPubMessage();
        subPubMessage.setAppName(systemConfigComponent.getAppName());
//        subPubMessage.setKey(SubPubConstants.AUTH_DATA);
        subPubMessage.setKey(key);
        subPubMessage.setData(data);
        subPubMessage.setTenantId(tenantId);
        logger.info("publish message={}",subPubMessage);
        messagePublisher.publish(subPubMessage);
    }
}
